#!/bin/sh
#pClock
#Copyright 2011, 2012
#Sigmund Berglund - Valåmoen, Norway.

#------------------------------
#This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 2.

#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. <http://www.gnu.org/licenses/>.
#------------------------------


export TEXTDOMAIN=pclock #for gettext

export APPDIR=`dirname $0`
[ $APPDIR = '.' ] && export APPDIR=`pwd`
export WORKDIR=$HOME/.pclock/tmp
if [ ! -d $WORKDIR ]; then
	mkdir -p $WORKDIR
	echo true > $WORKDIR/HUNDREDTHS
	echo '/usr/share/audio/beep_high.wav' > $WORKDIR/COUNTDOWN_ACTION
	echo '/usr/share/audio/2barks.wav' > $WORKDIR/ALARM
fi

#configs
TMP=`grep VERSION $HOME/.pclock/pclockrc 2> /dev/null` #check current version
if [ ! -f $HOME/.pclock/pclockrc ] || [ `echo $TMP | sed -e s/^.*\=//g | tr -d "."` != 072 > /dev/null 2>&1 ]; then
	#guess correct fontsize
	TMP=`xrdb -query | grep dpi | awk '{print $2}'` #check global fontsize
	FONTSIZE=$((4100/$TMP))
	#---
	echo 'export VERSION=0.7.2
export FONTSIZE='$FONTSIZE'
export COLOR1="black"
export COLOR2="gold"' > $HOME/.pclock/pclockrc
	chmod 722 $HOME/.pclock/pclockrc
fi
. $HOME/.pclock/pclockrc

#initials
rm -f $WORKDIR/end_while #start progessbar loop
echo -n > $WORKDIR/end_while_fullscreen #stop fullscreen loop
date +%s%N > $WORKDIR/seconds_start #save the time in nanoseconds (from 1970) to tempfile
echo -n > $WORKDIR/resultlist
echo analog > $WORKDIR/clock_mode
echo "$(gettext 'pClock uses the program pSchedule 
to keep track of alarming. Click the 
Define-Alarm button and you will 
get new options to define time and 
action. By default, pMusic plays 
the defined audio track.')" > $WORKDIR/alarm_txt
export MYGMT="`readlink /etc/localtime | cut -d/ -f6 | sed -e 's,^GMT-,GMTx,' -e 's,^GMT+,GMT-,' -e 's,^GMTx,GMT+,'`"
NOTEBOOK_TAB=0
echo 0 > $WORKDIR/pclock-tab
NOTEBOOK_TOOLS_TAB=0
echo 0 > $WORKDIR/pclock-tools_tab
echo 0 > $WORKDIR/COUNTDOWN
echo '' > $WORKDIR/COUNTDOWN_LOOPS

#gtk-theme
echo 'style "monoBig" { font_name="Mono bold '$FONTSIZE'" base[NORMAL]="'$COLOR1'" text[NORMAL]="'$COLOR2'" }
widget "*timer" style "monoBig"' > $WORKDIR/gtkrc
export GTK2_RC_FILES=$WORKDIR/gtkrc:~/.gtkrc-2.0 #include theme stuff with system themes



###############################################
#                                             #
#              F U N C T I O N S              #
#                                             #
###############################################


analogclock(){
	#clock size
	if [ ! -f $WORKDIR/end_while_fullscreen ]; then
		SCREEN_HEIGHT=`xwininfo -root | grep -m 1 '\geometry' | cut -f4 -d ' ' | cut -f2 -d 'x' | cut -f1 -d '+'`
		SIZE=$(($SCREEN_HEIGHT-100))
		OUTFILE=$WORKDIR/pclock-analog_big.svg
	else
		SIZE=300
		OUTFILE=$WORKDIR/pclock-analog.svg
	fi
	#---
	SEC="`date +%S`"
	SEC_ANGLE="`echo "$SEC * 6" | bc`"
	MIN="`date +%M`"
	MIN_ANGLE="`echo "$MIN * 6" | bc`"
	#calculate worldclock
	MYTIME="`echo "$MYGMT" | cut -c4- | sed -e 's,-,x,' -e 's,+,-,' -e 's,x,+,'`" #swap it for calculation
	NEWTIME="`echo "$1" | cut -d' ' -f1 | cut -c4-`"
	GMT_DIFF="$(($NEWTIME+$MYTIME))"
	HOUR_ANGLE=$((($(date +%l)+$GMT_DIFF)*30+($MIN_ANGLE/12)))
	#---
	echo '<?xml version="1.0" encoding="utf-8"?>
	<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="'$SIZE'" height="'$SIZE'" viewBox="0 0 512 512">
		<title>SVG analog clock</title>

		<circle fill="#777777" cx="256" cy="256" r="248" />
		<circle fill="#222222" cx="256" cy="256" r="245" />
		<circle fill="#E6F1DD" cx="256" cy="256" r="215.04" />
		<path fill="none" stroke="#000000" stroke-width="0.5" stroke-miterlimit="10" d="M173.297,437.792l-2.04,4.487 M211.854,455.793
			l1.058-4.79 M191.196,450.134l1.564-4.689 M137.297,416.566l-2.974,4.025 M89.375,374.943l4.104-2.936 M102.746,391.648l3.735-3.313
			 M121.187,403.292l-3.359,3.673 M233.025,459.357l0.556-4.931 M68.263,337.68l4.599-2.006 M60.717,317.62l4.82-1.522
			 M390.644,108.555l3.531-3.861 M405.353,123.472l3.904-3.461 M422.628,136.714l-4.267,3.05 M439.016,176.039l4.727-2.061
			 M446.364,195.594l4.921-1.557 M320.806,61.525l-1.667,5 M300.148,55.864l-1.131,5.116 M374.542,95.314l3.137-4.249 M278.977,52.301
			l-0.594,5.267 M316.302,446.398l1.491,4.715 M340.746,69.379l-2.169,4.773 M454.451,278.23l5.079,0.574 M416.708,374.511
			l4.057,2.996 M372.203,418.383l2.913,4.072 M403.453,390.635l3.685,3.367 M388.519,405.359l3.304,3.725 M335.881,439.049
			l1.972,4.521 M454.736,236.503l5.125-0.498 M455.966,299.977l-4.917-1.086 M450.306,320.634l-4.796-1.6 M442.451,340.574
			l-4.568-2.076 M456.625,214.78l-5.004,1.024 M66.593,192.657l-4.896-1.633 M52.473,232.854l5.117,0.578 M55.378,296.878l4.933-1.009
			 M52.142,275.652l5.087-0.494 M56.038,211.681l4.988,1.101 M195.8,65.57l-1.59-5.025 M216.002,60.337l-1.05-5.132 M176.246,72.896
			l-2.095-4.806 M236.177,51.969l0.513,5.274 M91.237,134.151l4.219,3.115 M136.886,89.203l3.072,4.297 M69.551,171.084l4.692,2.133
			 M108.717,121.176l-3.852-3.521 M120.183,102.574l3.471,3.916 M296.068,451.648l0.98,4.805 M275.824,459.688l-0.48-4.938" />
		<path d="M256.008,56.314l-2.52-5.14l5.12,0.04L256.008,56.314z M250.32,460.762l5.12,0.04l-2.52-5.141L250.32,460.762z
			 M361.9,80.655l-4.416-2.595l-0.388,5.712L361.9,80.655z M435.371,157.077l-2.526-4.454l-3.188,4.752L435.371,157.077z
			 M460.762,261.68l0.04-5.119l-5.14,2.521L460.762,261.68z M431.376,361.848l2.596-4.416l-5.711-0.387L431.376,361.848z
			 M354.869,435.4l4.454-2.524l-4.753-3.191L354.869,435.4z M157.278,76.512l-4.454,2.525l4.752,3.19L157.278,76.512z M80.775,149.899
			l-2.594,4.414l5.711,0.387L80.775,149.899z M51.214,253.392l-0.04,5.12l5.14-2.52L51.214,253.392z M76.48,354.666l2.525,4.455
			l3.192-4.754L76.48,354.666z M149.953,431.256l4.415,2.594l0.387-5.711L149.953,431.256z" />
		<path d="M256,50.688c-113.391,0-205.312,91.92-205.312,205.312c0,113.391,91.921,205.313,205.312,205.313
			c113.391,0,205.313-91.923,205.313-205.313C461.313,142.608,369.391,50.688,256,50.688z M256,461.056
			C142.75,461.056,50.944,369.25,50.944,256C50.944,142.751,142.75,50.944,256,50.944c113.249,0,205.056,91.807,205.056,205.056
			C461.056,369.25,369.249,461.056,256,461.056z" />

		<path d="M256,56.32C145.719,56.32,56.32,145.719,56.32,256c0,110.279,89.399,199.68,199.68,199.68
			c110.279,0,199.68-89.4,199.68-199.68C455.68,145.719,366.279,56.32,256,56.32z M256,455.424
			c-110.139,0-199.424-89.283-199.424-199.424c0-110.138,89.285-199.424,199.424-199.424c110.14,0,199.424,89.286,199.424,199.424
			C455.424,366.141,366.14,455.424,256,455.424z" />
		<path d="M350.393,87.04v47.923c0,0.717,0.159,1.267,0.48,1.65c0.464,0.538,1.031,0.807,1.703,0.807h4.656v2.88h-17.279v-2.88h3.984
			c0.832,0,1.445-0.313,1.848-0.941c0.398-0.626,0.601-1.465,0.601-2.515v-36.21c0-0.742-0.185-1.369-0.553-1.882
			c-0.367-0.512-0.839-0.768-1.416-0.768h-5.327v-2.649c2.499-0.512,4.526-1.196,6.081-2.054c1.556-0.857,2.925-1.978,4.111-3.36
			L350.393,87.04L350.393,87.04z" />
		<path d="M403.909,194.33c-0.733,1.51,2.886,2.265,10.863,2.265c1.194,0,2.049-0.499,2.56-1.497s1.093-3.763,1.747-8.294h1.656
			l-0.779,15.937h-21.493v-2.074l6.983-12.711c3.889-6.758,6.225-11.008,7.009-12.749c1.12-2.482,1.937-4.882,2.448-7.2
			c0.512-2.316,0.768-4.422,0.768-6.316c0-3.148-0.543-5.625-1.632-7.43c-1.089-1.805-2.6-2.708-4.536-2.708
			c-2.176,0-3.952,0.833-5.328,2.497s-2.063,3.43-2.063,5.299c0,0.64,0.104,1.127,0.313,1.459c0.192,0.333,0.608,0.627,1.248,0.883
			c1.616,0.615,2.424,2.125,2.424,4.531c0,1.306-0.271,2.388-0.814,3.245c-0.543,0.858-1.239,1.286-2.088,1.286
			c-0.978,0-1.832-0.723-2.567-2.17c-0.736-1.446-1.104-3.334-1.104-5.664c0-2.611,0.42-4.985,1.26-7.123
			c0.84-2.137,2.106-3.891,3.804-5.261c1.696-1.369,3.521-2.054,5.473-2.054c1.938,0,3.732,0.666,5.391,1.997
			c1.653,1.331,2.897,3.078,3.73,5.242c0.832,2.164,1.248,4.525,1.248,7.085c0,1.767-0.208,3.559-0.623,5.376
			c-0.415,1.818-1.021,3.521-1.819,5.107c-1.374,2.74-2.788,5.082-4.242,7.028l-6.327,8.524
			C405.813,190.988,404.646,192.819,403.909,194.33z" />
		<path d="M437.641,254.214c2.287,0.896,4.072,2.38,5.354,4.454c1.633,2.637,2.449,5.902,2.449,9.793c0,4.403-0.979,7.936-2.93,10.6
			c-2.225,3.07-5.12,4.607-8.688,4.607c-2.146,0-3.989-0.563-5.532-1.689c-1.543-1.125-2.7-2.648-3.468-4.568
			c-0.771-1.921-1.152-4.082-1.152-6.49c0-1.941,0.296-3.479,0.889-4.605s1.338-1.688,2.231-1.688c0.816,0,1.473,0.429,1.968,1.286
			c0.496,0.857,0.745,2.029,0.745,3.516c0,0.945-0.128,1.746-0.385,2.398c-0.256,0.654-0.704,1.262-1.344,1.824
			c-0.688,0.588-1.032,1.15-1.032,1.688c0,0.77,0.243,1.633,0.729,2.592c0.489,0.961,1.209,1.729,2.16,2.305
			c0.951,0.576,2.155,0.865,3.611,0.865c2.512,0,4.373-0.992,5.58-2.978c1.208-1.983,1.813-5.164,1.813-9.541
			c0-4.582-0.517-7.84-1.549-9.772c-1.031-1.933-2.563-2.898-4.596-2.898c-0.543,0-1.226,0.103-2.039,0.307
			c-0.656,0.154-1.129,0.23-1.416,0.23c-0.48,0-0.854-0.16-1.116-0.48c-0.265-0.32-0.396-0.723-0.396-1.209
			c0-0.538,0.124-0.954,0.371-1.249c0.246-0.293,0.65-0.441,1.213-0.441l1.318,0.077l1.151,0.077c2.032,0,3.561-0.831,4.585-2.492
			c1.023-1.661,1.536-4.229,1.536-7.705c0-4.267-0.489-7.302-1.465-9.104c-0.978-1.801-2.434-2.703-4.368-2.703
			c-1.775,0-3.192,0.511-4.248,1.532c-1.056,1.022-1.584,1.966-1.584,2.834c0,0.562,0.305,1.315,0.912,2.26
			c0.864,1.277,1.297,2.604,1.297,3.983c0,1.046-0.239,1.927-0.72,2.643c-0.481,0.716-1.098,1.072-1.851,1.072
			c-0.8,0-1.452-0.487-1.956-1.459s-0.755-2.33-0.755-4.07c0-2.714,0.607-5.12,1.823-7.219c1.729-2.969,4.2-4.455,7.416-4.455
			c3.009,0,5.407,1.293,7.199,3.878c1.792,2.586,2.688,5.786,2.688,9.6c0,2.74-0.482,5.108-1.452,7.104
			C441.676,250.912,440.008,252.678,437.641,254.214z" />
		<path d="M403.872,312.32h1.56v36.326h5.521v3.265h-5.521v8.18c0,0.718,0.178,1.293,0.528,1.729c0.512,0.589,1.104,0.882,1.775,0.882
			h2.04v2.881h-13.32v-2.881h2.472c0.833,0,1.444-0.281,1.836-0.847c0.395-0.563,0.588-1.381,0.588-2.457v-7.487h-13.582v-3.681
			L403.872,312.32z M401.352,322.666l-11.592,25.979h11.592V322.666z" />
		<path d="M330.336,373.76c4.048,1.484,7.354,2.229,9.913,2.229c1.199,0,2.327-0.188,3.384-0.56c1.058-0.369,1.851-0.799,2.376-1.285
			c0.272-0.229,0.448-0.347,0.528-0.347c0.159,0,0.239,0.192,0.239,0.577c0,0.896-0.36,2.079-1.08,3.553
			c-0.722,1.471-1.7,2.644-2.94,3.514c-1.239,0.871-2.667,1.307-4.285,1.307c-1.872,0-4.104-0.461-6.693-1.383l-0.783,14.67
			c1.216-0.975,2.375-1.703,3.478-2.188c1.104-0.488,2.151-0.73,3.144-0.73c3.136,0,5.77,1.664,7.904,4.992
			c2.136,3.328,3.201,7.437,3.201,12.326c0,5.428-1.135,9.894-3.405,13.4c-2.272,3.507-5.219,5.26-8.832,5.26
			c-2.771,0-5.021-1.26-6.759-3.779c-1.734-2.522-2.604-5.523-2.604-9.006c0-2.101,0.276-3.72,0.828-4.856
			c0.552-1.14,1.26-1.71,2.124-1.71c0.848,0,1.54,0.455,2.075,1.363c0.534,0.91,0.804,2.093,0.804,3.553
			c0,1.102-0.132,1.958-0.396,2.572c-0.265,0.615-0.748,1.129-1.45,1.535c-0.705,0.438-1.058,0.938-1.058,1.498
			c0,0.819,0.239,1.724,0.72,2.708s1.204,1.792,2.174,2.417c0.968,0.629,2.073,0.942,3.321,0.942c2.449,0,4.316-1.217,5.604-3.648
			c1.288-2.433,1.933-6.388,1.933-11.864c0-4.992-0.68-8.641-2.04-10.944c-1.357-2.304-3.096-3.456-5.208-3.456
			c-0.96,0-1.92,0.281-2.881,0.844c-0.96,0.564-1.92,1.409-2.88,2.535c-0.111,1.792-0.313,2.989-0.601,3.592
			c-0.287,0.603-0.656,0.902-1.104,0.902c-0.32,0-0.576-0.18-0.768-0.538c-0.193-0.358-0.288-0.856-0.288-1.497
			c0-1.127,0.327-2.24,0.982-3.342c0.192-0.332,0.327-0.627,0.407-0.883l0.559-11.288C330.202,382.285,330.32,377.959,330.336,373.76z" />
		<path d="M249.58,419.174c1.248-2.662,2.56-4.568,3.936-5.721c1.376-1.153,2.888-1.729,4.537-1.729c2.656,0,4.885,1.438,6.684,4.318
			c1.801,2.881,2.699,6.522,2.699,10.925c0,5.3-1.063,9.703-3.189,13.21s-4.762,5.263-7.896,5.263c-2.208,0-4.176-0.94-5.904-2.822
			c-1.729-1.883-3.14-4.82-4.236-8.813c-1.097-3.993-1.644-8.651-1.644-13.978c0-5.761,0.672-10.981,2.016-15.668
			c1.345-4.685,3.06-8.197,5.148-10.539c2.088-2.345,4.268-3.517,6.542-3.517c2.336,0,4.318,1.024,5.953,3.071
			c1.357,1.664,2.037,3.661,2.037,5.99c0,1.563-0.299,2.842-0.898,3.841c-0.602,0.998-1.332,1.498-2.195,1.498
			c-0.8,0-1.484-0.422-2.053-1.27c-0.568-0.844-0.854-1.842-0.854-2.993c0-0.769,0.192-1.64,0.576-2.611
			c0.354-0.896,0.529-1.603,0.529-2.112c0-0.716-0.219-1.306-0.648-1.767c-0.574-0.589-1.367-0.884-2.376-0.884
			c-1.552,0-2.967,0.781-4.248,2.343c-1.28,1.563-2.364,4.243-3.252,8.045c-0.888,3.802-1.333,8.07-1.333,12.808
			C249.508,416.756,249.532,417.792,249.58,419.174z M256.732,415.488c-2,0-3.637,1.158-4.908,3.477
			c-1.272,2.316-1.908,5.573-1.908,9.771c0,4.557,0.616,8.04,1.848,10.443c1.232,2.407,2.8,3.608,4.704,3.608
			c1.89,0,3.408-1.138,4.562-3.417c1.15-2.278,1.729-5.685,1.729-10.216c0-4.813-0.532-8.296-1.597-10.444
			C260.098,416.564,258.621,415.488,256.732,415.488z" />

		<path d="M164.936,373.779c1.568,0.641,3.265,0.961,5.088,0.961h14.928v1.768c-2.192,6.17-3.832,11.385-4.92,15.646
			c-1.089,4.263-1.912,8.34-2.472,12.229c-0.56,3.895-0.928,8.426-1.104,13.596c-0.16,4.273-0.312,6.916-0.456,7.929
			c-0.145,1.012-0.436,1.793-0.875,2.343s-1.012,0.825-1.716,0.825c-0.8,0-1.424-0.437-1.872-1.307
			c-0.448-0.867-0.672-2.238-0.672-4.107c0-1.663,0.184-3.743,0.552-6.239c0.368-2.497,1.208-6.229,2.52-11.195
			c1.744-6.654,3.256-11.891,4.537-15.705l3.312-9.752h-13.009c-0.961,0-1.676,0.537-2.148,1.611
			c-0.472,1.076-1.037,4.289-1.692,9.639h-1.729L164.936,373.779z" />
		<path d="M116.804,335.41c2.56,1.477,4.408,3.412,5.544,5.807c1.135,2.395,1.704,5.279,1.704,8.66c0,4.121-0.824,7.717-2.472,10.789
			c-2.128,3.969-5.16,5.953-9.096,5.953c-3.473,0-6.216-1.338-8.232-4.014c-2.016-2.672-3.024-6.008-3.024-10.002
			c0-2.994,0.58-5.609,1.74-7.852c1.16-2.238,3.164-4.141,6.012-5.7c-4.208-2.896-6.312-7.364-6.312-13.407
			c0-3.916,0.948-7.289,2.843-10.119c1.896-2.828,4.3-4.242,7.212-4.242c2,0,3.739,0.551,5.22,1.65
			c1.48,1.102,2.608,2.601,3.384,4.496c0.776,1.896,1.164,3.879,1.164,5.951C122.492,328.2,120.597,332.208,116.804,335.41z
			 M110.78,340.125c-2.272,1.537-3.848,3.266-4.728,5.186c-0.88,1.92-1.32,4.273-1.32,7.063c0,3.634,0.667,6.456,2.004,8.466
			c1.336,2.008,3.26,3.014,5.772,3.014c2.656,0,4.72-1.006,6.192-3.016c1.472-2.009,2.208-4.665,2.208-7.967
			c0-2.995-0.636-5.262-1.908-6.797C117.729,344.538,114.988,342.557,110.78,340.125z M114.956,334.412
			c1.681-1.536,2.808-3.055,3.385-4.553c0.576-1.5,0.864-3.465,0.864-5.897c0-3.202-0.568-5.674-1.704-7.416
			c-1.136-1.741-2.784-2.612-4.944-2.612c-2.096,0-3.729,0.813-4.896,2.44c-1.168,1.627-1.752,3.748-1.752,6.36
			c0,2.59,0.507,4.65,1.522,6.188C108.444,330.46,110.954,332.29,114.956,334.412z" />
		<path d="M84.368,255.021c-1.04,2.508-2.256,4.397-3.648,5.665c-1.392,1.268-2.952,1.9-4.68,1.9c-2.4,0-4.58-1.346-6.54-4.033
			c-1.96-2.687-2.94-6.643-2.94-11.865c0-5.095,1.084-9.427,3.252-12.999c2.168-3.571,4.757-5.356,7.764-5.356
			c3.312,0,6.072,2.202,8.28,6.605s3.312,10.586,3.312,18.547c0,6.605-0.612,12.179-1.836,16.723s-2.844,7.924-4.86,10.138
			c-2.017,2.215-4.289,3.32-6.816,3.32c-2.448,0-4.364-0.863-5.748-2.592c-1.384-1.727-2.076-3.795-2.076-6.201
			c0-1.588,0.285-2.867,0.855-3.84c0.571-0.973,1.29-1.46,2.156-1.46c0.789,0,1.454,0.423,2.001,1.268
			c0.546,0.845,0.819,1.856,0.819,3.033c0,0.845-0.253,1.78-0.759,2.804c-0.31,0.639-0.465,1.189-0.465,1.65
			c0,0.716,0.264,1.338,0.792,1.861s1.376,0.787,2.543,0.787c1.664,0,3.128-0.734,4.392-2.207s2.288-3.893,3.072-7.259
			c0.785-3.364,1.176-7.726,1.176-13.075L84.368,255.021z M77.648,231.097c-1.792,0-3.265,1.197-4.417,3.59
			c-1.152,2.394-1.729,5.997-1.729,10.81c0,4.403,0.577,7.686,1.729,9.85c1.152,2.163,2.656,3.245,4.512,3.245
			c1.84,0,3.388-1.164,4.644-3.495c1.257-2.329,1.884-5.593,1.884-9.792c0-4.352-0.652-7.808-1.956-10.368
			C81.012,232.378,79.456,231.097,77.648,231.097z" />
		<path d="M108.063,148.941v47.923c0,0.717,0.16,1.267,0.48,1.65c0.464,0.538,1.032,0.807,1.704,0.807h4.656v2.88H97.622v-2.88h3.984
			c0.833,0,1.448-0.313,1.848-0.941c0.4-0.626,0.6-1.465,0.6-2.515v-36.211c0-0.742-0.184-1.369-0.552-1.882
			c-0.368-0.512-0.84-0.768-1.417-0.768h-5.328v-2.649c2.5-0.512,4.527-1.196,6.082-2.054c1.556-0.857,2.924-1.978,4.111-3.36H108.063
			L108.063,148.941z M131.338,147.942c3.12,0,5.72,1.946,7.8,5.837c2.576,4.787,3.864,12.045,3.864,21.773
			c0,9.037-1.121,15.916-3.36,20.64c-2.241,4.723-4.944,7.084-8.112,7.084c-3.056,0-5.608-1.983-7.656-5.953
			c-2.56-4.992-3.84-12.198-3.84-21.619c0-8.96,1.208-16.089,3.624-21.389C125.594,150.067,128.154,147.942,131.338,147.942z
			 M131.482,150.707c-2.113,0-3.76,1.62-4.944,4.858c-1.184,3.239-1.776,9.953-1.776,20.141c0,9.959,0.612,16.602,1.836,19.929
			c1.224,3.328,2.868,4.992,4.932,4.992c2.08,0,3.68-1.536,4.801-4.608c1.328-3.661,1.992-10.342,1.992-20.044
			c0-10.035-0.624-16.755-1.872-20.161C135.203,152.41,133.545,150.707,131.482,150.707z" />
		<path d="M171.782,87.04v47.923c0,0.717,0.16,1.267,0.48,1.65c0.464,0.538,1.032,0.807,1.704,0.807h4.656v2.88h-17.28v-2.88h3.984
			c0.833,0,1.448-0.313,1.849-0.941c0.4-0.626,0.6-1.465,0.6-2.515v-36.21c0-0.742-0.185-1.369-0.552-1.882
			c-0.368-0.512-0.839-0.768-1.416-0.768h-5.328v-2.649c2.5-0.512,4.527-1.196,6.082-2.054c1.556-0.857,2.925-1.978,4.111-3.36
			L171.782,87.04L171.782,87.04z M195.322,87.04v47.923c0,0.717,0.16,1.267,0.48,1.65c0.464,0.538,1.032,0.807,1.704,0.807h4.656v2.88
			h-17.28v-2.88h3.984c0.833,0,1.448-0.313,1.848-0.941c0.4-0.626,0.6-1.465,0.6-2.515v-36.21c0-0.742-0.184-1.369-0.552-1.882
			c-0.368-0.512-0.84-0.768-1.416-0.768h-5.328v-2.649c2.5-0.512,4.526-1.196,6.082-2.054c1.555-0.857,2.923-1.978,4.11-3.36
			L195.322,87.04L195.322,87.04z" />
		<path d="M246.59,67.559v47.923c0,0.717,0.16,1.267,0.48,1.651c0.464,0.538,1.032,0.807,1.704,0.807h4.656v2.88h-17.28v-2.88h3.984
			c0.833,0,1.448-0.313,1.849-0.941c0.4-0.626,0.6-1.465,0.6-2.515V78.272c0-0.742-0.185-1.369-0.552-1.882
			c-0.368-0.512-0.839-0.768-1.416-0.768h-5.328v-2.649c2.5-0.512,4.528-1.196,6.082-2.054c1.556-0.857,2.925-1.977,4.112-3.359
			L246.59,67.559L246.59,67.559z" />
		<path d="M264.889,112.41c-0.734,1.51,2.885,2.265,10.861,2.265c1.195,0,2.05-0.499,2.561-1.497c0.51-0.999,1.094-3.763,1.748-8.294
			h1.655l-0.78,15.937h-21.492v-2.074l6.984-12.711c3.889-6.758,6.225-11.008,7.008-12.749c1.121-2.482,1.938-4.883,2.448-7.2
			c0.513-2.316,0.769-4.422,0.769-6.316c0-3.148-0.543-5.626-1.633-7.43c-1.088-1.804-2.6-2.708-4.535-2.708
			c-2.176,0-3.949,0.833-5.326,2.497s-2.064,3.431-2.064,5.299c0,0.64,0.105,1.127,0.313,1.459c0.193,0.333,0.607,0.628,1.248,0.884
			c1.615,0.614,2.425,2.125,2.425,4.531c0,1.306-0.271,2.388-0.816,3.245c-0.543,0.858-1.237,1.286-2.088,1.286
			c-0.976,0-1.832-0.723-2.565-2.17c-0.736-1.446-1.104-3.334-1.104-5.664c0-2.611,0.42-4.985,1.261-7.123
			c0.84-2.137,2.107-3.891,3.805-5.261c1.695-1.369,3.521-2.054,5.473-2.054c1.938,0,3.732,0.666,5.391,1.997
			c1.653,1.332,2.897,3.078,3.73,5.242c0.833,2.164,1.248,4.525,1.248,7.085c0,1.767-0.208,3.559-0.624,5.376
			c-0.415,1.817-1.021,3.521-1.82,5.106c-1.375,2.74-2.789,5.083-4.241,7.028l-6.328,8.525
			C266.79,109.068,265.623,110.899,264.889,112.41z" />
		<polygon id="clockhand_hour" points="252.5,156 252.5,256 252.5,286 259.5,286 259.5,256 259.5,156" transform="rotate('$HOUR_ANGLE',256,256)"/>
		<polygon id="clockhand_min" points="253.5,115 253.5,256 253.5,301 258.5,301 258.5,256 258.5,115" transform="rotate('$MIN_ANGLE',256,256)"/>
		<polygon id="clockhand_sec" points="258,271 257,271 257,256 257,126 255,126 255,256 255,271 254,271 252,321 260,321" transform="rotate('$SEC_ANGLE',256,256)"/>
		<circle fill="#000000" cx="256" cy="256" r="8" />
	</svg>' > $OUTFILE
}


func_time(){ #called as input for the entry
	export GTK2_RC_FILES=/root/.gtkrc-2.0 #reset gtk
	case "`cat $WORKDIR/clock_mode`" in
	null)
		echo '00:00:00' > $WORKDIR/timer
		echo; echo 100 #update clock
		sleep 1
		;;
	analog)
		echo; echo 100 #update clock
		sleep 0.95
		;;
	clock12)
		NANOSEC=`date +%N | head -c 2`
		echo $NANOSEC > $WORKDIR/NANOSEC #for fullscreen
		if [ $NANOSEC -lt 25 ]; then
			date +%I:%M:%S > $WORKDIR/timer
			echo; echo 100 #update clock
			sleep 0.6 #no use in looping until we are closer to whole second
		fi
		;;
	clock24)
		NANOSEC=`date +%N | head -c 2`
		echo $NANOSEC > $WORKDIR/NANOSEC #for fullscreen
		if [ $NANOSEC -lt 25 ]; then
			date +%H:%M:%S > $WORKDIR/timer
			echo; echo 100 #update clock
			sleep 0.6 #no use in looping until we are closer to whole second
		fi
		;;
	stopwatch)
		date +%s%N > $WORKDIR/seconds_now
		SEC_START=$(<$WORKDIR/seconds_start)
		echo "$(($(<$WORKDIR/seconds_now)-$SEC_START))" | tail -c 10 | head -c 2 > $WORKDIR/NANOSEC #this file is used by fullscreen
		NANOSEC=($(<$WORKDIR/NANOSEC))
		if [ $NANOSEC -lt 25 ]; then
			TIME_SEC=`echo "($(<$WORKDIR/seconds_now) - $SEC_START) / 1000000000" | bc 2> /dev/null`
			printf "%02d:%02d:%02d" $((TIME_SEC/3600)) $((TIME_SEC%3600/60)) $((TIME_SEC%60)) > $WORKDIR/timer
			echo; echo 100 #update clock
			sleep 0.6 #no use in looping until we are closer to whole second
		fi
		;;
	countdown_start)
		COUNTDOWN_SEC="`cat $WORKDIR/COUNTDOWN`"
		printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > $WORKDIR/timer
		echo; echo 100 #update clock
		sleep 1
		;;
	countdown)
		date +%s%N > $WORKDIR/seconds_now
		SEC_NOW=$(($(<$WORKDIR/seconds_now)-1000000000))
		SEC_START=$(<$WORKDIR/seconds_start)
		NANOSEC=`echo "$(($SEC_START-$SEC_NOW))" | tail -c 10 | head -c 2`
		if [ $NANOSEC -gt 75 ]; then
			TIME_SEC=`echo "($SEC_NOW - $SEC_START) / 1000000000" | bc 2> /dev/null`
			COUNTDOWN_SEC="$((($SEC_START-$SEC_NOW)/1000000000))"
			printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > $WORKDIR/timer
			echo; echo 100 #update clock
			echo $COUNTDOWN_SEC > $WORKDIR/COUNTDOWN
			if [ $COUNTDOWN_SEC = 0 ]; then
				TMP="$(<$WORKDIR/COUNTDOWN_ACTION)"
				if [ -f "$TMP" ]; then #file is defined --> try to play it 
					if [ "`echo "$TMP" | grep -E "\.wav$|\.au$"`" ]; then
						aplay "$TMP" &
					else
						ffmpeg -i "$TMP" -f au - | aplay &
					fi
				else
					chmod 722 $WORKDIR/COUNTDOWN_ACTION
					$WORKDIR/COUNTDOWN_ACTION &
				fi
				TMP="$(<$WORKDIR/COUNTDOWN_START)"
				echo $(($(date +%s)+$TMP))$(date +%N) > $WORKDIR/seconds_start
				#---
				TMP="$(<$WORKDIR/COUNTDOWN_LOOPS)"
				echo $(($TMP+1)) > $WORKDIR/COUNTDOWN_LOOPS

			fi
			sleep 0.6 #no use in looping until we are closer to whole second
		fi
		;;
	esac
}

timestamp(){
	date +%s%N > $WORKDIR/seconds_now
	SEC_SINCE_START="$((($(<$WORKDIR/seconds_now)-$(<$WORKDIR/seconds_start))/1000000000))"
	NANOSEC=`echo "$(($(<$WORKDIR/seconds_now)-$(<$WORKDIR/seconds_start)))" | tail -c 10 | head -c 2`
	TIME=`printf "%02d:%02d:%02d" $((SEC_SINCE_START/3600)) $((SEC_SINCE_START%3600/60)) $((SEC_SINCE_START%60))`
	[ -f $WORKDIR/HUNDREDTHS ] && TIME=${TIME}.$NANOSEC
	#laptime
	TIME_PREV=`tail -n1 $WORKDIR/resultlist | cut -d'|' -f3` #previous timestamp
	if [ $TIME_PREV = 00:00:00 ]; then
		LAPTIME=$TIME
	else
		HOUR=`echo $TIME_PREV | cut -d: -f1`
		MIN=`echo $TIME_PREV | cut -d: -f2`
		SEC=`echo $TIME_PREV | cut -d: -f3 | cut -d. -f1`
		NSEC=`echo $TIME_PREV | cut -d. -f2`
		if [ ! -f $WORKDIR/HUNDREDTHS ]; then NSEC=0; NANOSEC=0; fi
		LAPTIME_PREV_SEC=`echo "($HOUR*3600)+($MIN*60)+($SEC)" | bc`
		LAPTIME_SEC=`echo ${SEC_SINCE_START}.${NANOSEC} - ${LAPTIME_PREV_SEC}.${NSEC} | bc | cut -d. -f1`
		LAPTIME_NSEC=`echo ${SEC_SINCE_START}.${NANOSEC} - ${LAPTIME_PREV_SEC}.${NSEC} | bc | cut -d. -f2`
		LAPTIME=`printf "%02d:%02d:%02d" $((LAPTIME_SEC/3600)) $((LAPTIME_SEC%3600/60)) $((LAPTIME_SEC%60))`
		[ -f $WORKDIR/HUNDREDTHS ] && LAPTIME=${LAPTIME}.$LAPTIME_NSEC
	fi
	#---
	TMP=`tail -n1 $WORKDIR/resultlist | cut -d'|' -f1`
	if [ ! "$TMP" ]; then LAPNR=1; else LAPNR=$((($TMP)+1)); fi
	echo "$LAPNR|$LAPTIME|$TIME" >> $WORKDIR/resultlist
}

fullscreen (){
	rm -f $WORKDIR/end_while_fullscreen #activate loop
	#geometry
	HEIGHT=`xwininfo -root | grep -m 1 '\geometry' | cut -f4 -d ' ' | cut -f2 -d 'x' | cut -f1 -d '+'`
	WIDTH=`xwininfo -root | grep -m 1 '\geometry' | cut -f4 -d ' ' | cut -f1 -d 'x'`
	#gtk-theme
	echo 'style "monohuge" { font_name="Mono bold '`echo "( $WIDTH * $FONTSIZE ) / 280" | bc`'" bg[NORMAL]="'$COLOR1'" base[NORMAL]="'$COLOR1'" fg[NORMAL]="'$COLOR2'" text[NORMAL]="'$COLOR2'" }
	widget "*" style "monohuge"' > $WORKDIR/gtkrc2
	export GTK2_RC_FILES=$WORKDIR/gtkrc2:/root/.gtkrc-2.0 #include theme stuff with system themes
	#gui  	
	if [ "$RADIOBUTTON_ANALOG_CLOCK" = "true" ]; then
		export curtain='<window title="pClock '$LOC_FULLSCREEN'" width-request="'$WIDTH'" height-request="'$HEIGHT'" decorated="false">
		<vbox>
		 <hbox width-request="1">
		  <button relief="2" can-focus="no"><input file stock="gtk-close"></input><action>exit:EXIT</action></button>
		  <timer visible="false">
	       <action>analogclock $GMT</action>
		   <action>refresh:PIX_ANALOG_CLOCK</action>
		  </timer>
		 </hbox>
		 <pixmap space-expand="false" space-fill="false"><variable>PIX_ANALOG_CLOCK</variable><input file>'$WORKDIR'/pclock-analog_big.svg</input></pixmap>
		</vbox>
		<action signal="hide">exit:EXIT</action>
		</window>'
	else
		export curtain='<window title="pClock '$LOC_FULLSCREEN'" width-request="'$WIDTH'" height-request="'$HEIGHT'" decorated="false">
		<vbox>
		 <hbox width-request="1">
		  <button relief="2" can-focus="no"><input file stock="gtk-close"></input><action>exit:EXIT</action></button>
		  <vbox height-request="1">
		   <progressbar width-request="1" height-request="1" >
		    <input>while [ ! -f '$WORKDIR'/end_while_fullscreen ]; do if [ `cat '$WORKDIR'/NANOSEC` -lt 25 ]; then echo; echo 100; sleep 0.4; fi; sleep 0.05; done</input>
		    <action>refresh:TIMER</action>
		   </progressbar>
		  </vbox>
		 </hbox>
		 <entry can-focus="no" width-request="'$WIDTH'" height-request="'$((HEIGHT/3))'">
		  <variable>TIMER</variable>
		  <input>cat '$WORKDIR'/timer</input>
		  <action signal="button-press-event">exit:EXIT</action>
		 </entry>
		</vbox>
		<action signal="hide">exit:EXIT</action>
		</window>'
	fi
	gtkdialog4 -p curtain --center
	echo end > $WORKDIR/end_while_fullscreen
	export GTK2_RC_FILES=$WORKDIR/gtkrc:/root/.gtkrc-2.0 #reset gtk-theme
}

preferences(){
	export GTK2_RC_FILES=/root/.gtkrc-2.0 #reset gtk
	export pClock_pref='
	<window title="pClock - '$(gettext "Preferences")'">
	 <vbox>
	  <hbox>
	   <text><label>'$(gettext "Font size")'</label></text>
	   <entry><variable>FONTSIZE</variable><default>'$FONTSIZE'</default></entry>
	  </hbox>
	  <hbox>
	   <text><label>'$(gettext "Background Colour")'</label></text>
	   <entry><variable>COLOR1</variable><default>'$COLOR1'</default></entry>
	  </hbox>
	  <hbox>
	   <text><label>'$(gettext "Foreground Colour")'</label></text>
	   <entry><variable>COLOR2</variable><default>'$COLOR2'</default></entry>
	  </hbox>
	  <hbox>
	   <text use-markup="true"><label>"<b>'$(gettext "Restart pClock to activate changes")'</b>    "</label></text>
	   <button cancel></button>
	   <button ok>
	    <action>echo -e "export VERSION=$VERSION\nexport FONTSIZE=$FONTSIZE\nexport COLOR1=\"$COLOR1\"\nexport COLOR2=\"$COLOR2\"" > '$HOME'/.pclock/pclockrc</action>
	    <action>EXIT:exit</action>
	    </button>
	  </hbox>
	 </vbox>
	</window>'
	gtkdialog3 -p pClock_pref
}

system_time (){
	export GTK2_RC_FILES=/root/.gtkrc-2.0 #reset gtk
	set-time-for-puppy &
}

alarm(){
	export GTK2_RC_FILES=/root/.gtkrc-2.0 #reset gtk
	pschedule -s -e "pmusic -B '$ALARM'" "pClock Alarm" &
}

export -f analogclock func_time timestamp fullscreen preferences system_time alarm


###############################################
#                                             #
#                  A B O U T                  #
#                                             #
###############################################

export about="
<window title=\"pClock - about\">
 <notebook labels=\"About|License\">
  <frame>
   <text><label>\"\"</label></text>
   <progressbar>
    <input>echo 0; echo \"pClock $VERSION\"</input>
   </progressbar>
   <text use-markup=\"true\"><label>\"<b>Sigmund Berglund, Copyright 2011-2014</b>\"</label></text>
   <text height-request=\"30\"><label>\"\"</label></text>
   <button>
    <label>\"http://www.murga-linux.com/puppy/viewtopic.php?p=493137\"</label>
    <action>defaulthtmlviewer http://www.murga-linux.com/puppy/viewtopic.php?p=493137#493137 &</action>
   </button>
  </frame>
  <frame>
   <text use-markup=\"true\"><label>\"pClock is released under the <b>GNU General Public License</b> (GPL). You have the right to use and modify this software in any way you like, so long as any derivative works remain under a GPL license.\"</label></text>
   <text use-markup=\"true\"><label>\"This program is distributed in the hope that it will be useful, but <b><span color='"'red'"'>WITHOUT ANY WARRANTY</span></b>. See the GNU General Public License homepage for more details.\"</label></text>
   <hbox>
    <button>
     <label>\"http://www.gnu.org/licenses/\"</label>
     <action>defaulthtmlviewer http:\/\/www.gnu.org\/licenses\/</action>
    </button>
   </hbox>
  </frame>
 </notebook>
</window>"



###############################################
#                                             #
#               M A I N   G U I               #
#                                             #
###############################################

export pClock='
<window title="pClock">
<vbox>
  <menubar>
   <menu>
    <menuitem stock="gtk-preferences">
     <action>preferences</action>
    </menuitem>
    <menuitem label="'$(gettext "Set system time")'">
     <action>system_time &</action>
    </menuitem>
    <menuitem label="'$(gettext "Set system time zone")'">
     <action>countrywizard &</action>
    </menuitem>
    <menuitemseparator></menuitemseparator>
    <menuitem  stock="gtk-quit" >
     <action>exit:EXIT</action>
    </menuitem>
    <label>'$(gettext "File")'</label>
   </menu>

   <menu>
    <menuitem  stock="gtk-fullscreen">
     <action>fullscreen &</action>
    </menuitem>
    <label>'$(gettext "View")'</label>
   </menu>
   <menu>
    <menuitem  stock="gtk-about">
     <action>launch:about</action>
    </menuitem>
    <label>'$(gettext "Help")'</label>
   </menu>
  </menubar>
  
  <hbox homogeneous="true">
   <hbox>
    <radiobutton label=" '$(gettext "Clock")' " draw_indicator="false">
     <action>echo analog > '$WORKDIR'/clock_mode</action>
     <variable>RADIOBUTTON_ANALOG_CLOCK</variable>
     <action>echo 0 > '$WORKDIR'/pclock-tab</action>
     <action>refresh:NOTEBOOK</action>
     <action>refresh:NOTEBOOK_TOOLS</action>
    </radiobutton>
    <radiobutton label=" '$(gettext "Stopwatch")' " draw_indicator="false">
     <action>echo null > '$WORKDIR'/clock_mode</action>
     <action>echo 1 > '$WORKDIR'/pclock-tools_tab</action>
     <action>echo 1 > '$WORKDIR'/pclock-tab</action>
     <action>refresh:NOTEBOOK</action>
     <action>refresh:NOTEBOOK_TOOLS</action>
    </radiobutton>
    <radiobutton label=" '$(gettext "Countdown")' " draw_indicator="false">
     <action>echo countdown_start > '$WORKDIR'/clock_mode</action>
     <action>echo 2 > '$WORKDIR'/pclock-tools_tab</action>
     <action>echo 1 > '$WORKDIR'/pclock-tab</action>
     <action>disable:BUTTON_COUNTDOWN_START</action>
     <action>disable:BUTTON_COUNTDOWN_STOP</action>
     <action>disable:BUTTON_COUNTDOWN_PAUSE</action>
     <action>refresh:NOTEBOOK</action>
     <action>refresh:NOTEBOOK_TOOLS</action>
    </radiobutton>
    <radiobutton label=" '$(gettext "Alarm")' " draw_indicator="false">
     <action>echo clock24 > '$WORKDIR'/clock_mode</action>
     <action>echo 3 > '$WORKDIR'/pclock-tools_tab</action>
     <action>echo 1 > '$WORKDIR'/pclock-tab</action>
     <action>refresh:NOTEBOOK</action>
     <action>refresh:NOTEBOOK_TOOLS</action>
    </radiobutton>
##    <radiobutton label=" '$LOC_SYSTEM' " draw_indicator="false">
##     <action>echo 0 > '$WORKDIR'/pclock-tools_tab</action>
##     <action>echo 1 > '$WORKDIR'/pclock-tab</action>
##     <action>refresh:NOTEBOOK</action>
##     <action>refresh:NOTEBOOK_TOOLS</action>
##    </radiobutton>
   </hbox>
  </hbox>
  <hseparator></hseparator>
  <hbox width-request="1">
   <progressbar width-request="1" height-request="1" >
    <input>while [ ! -f '$WORKDIR'/end_while ]; do func_time; sleep 0.05; done</input>
    <action>refresh:TIMER</action>
    <action>analogclock $GMT</action>
    <action>refresh:PIX_ANALOG_CLOCK</action>
   </progressbar>
  </hbox>
  <notebook show-tabs="false" show-border="false" page="'$NOTEBOOK_TAB'" space-expand="true" space-fill="true">
   <vbox>
    <combobox>
     <variable>GMT</variable>
     <item>'$MYGMT'</item>
     <item>GMT-12 Eniwetok</item>
     <item>GMT-11 Samoa</item>
     <item>GMT-10 Alaska,Hawaii</item>
     <item>GMT-9 Alaska</item>
     <item>GMT-8 Los_Angeles</item>
     <item>GMT-7 Alberta,Montana,Arizona</item>
     <item>GMT-6 Mexico_City,Saskatchewan</item>
     <item>GMT-5 Bogota,Lima,New_York</item>
     <item>GMT-4 Caracas,La_Paz,Canada</item>
     <item>GMT-3 Brasilia,Buenos_Aires,Georgetown</item>
     <item>GMT-2 mid-Atlantic</item>
     <item>GMT-1 Azores,CapeVerdes</item>
     <item>GMT+0 London,Dublin,Edinburgh,Lisbon,Reykjavik,Casablanca</item>
     <item>GMT+1 Paris,Berlin,Amsterdam,Brussels,Madrid,Stockholm,Oslo</item>
     <item>GMT+2 Athens,Helsinki,Istanbul,Jerusalem,Harare</item>
     <item>GMT+3 Kuwait,Nairobi,Riyadh,Moscow</item>
     <item>GMT+4 Abu_Dhabi,Muscat,Tblisi,Volgograd,Kabul</item>
     <item>GMT+5 Islamabad,Karachi</item>
     <item>GMT+6 Almaty,Dhaka</item>
     <item>GMT+7 Bangkok,Jakarta</item>
     <item>GMT+8 Perth,Singapore,Hongkong</item>
     <item>GMT+9 Tokyo</item>
     <item>GMT+10 Guam</item>
     <item>GMT+11 Magadan,Soloman_Is.</item>
     <item>GMT+12 Wellington,Fiji,Marshall_Islands</item>
     <item>GMT+13 Rawaki_Islands</item>
     <item>GMT+14 Line_Islands</item>
    </combobox>
    <text><label>""</label></text>
    <pixmap space-expand="false" space-fill="false"><variable>PIX_ANALOG_CLOCK</variable><input file>'$WORKDIR'/pclock-analog.svg</input></pixmap>
   </vbox>

   <vbox>
    <hbox homogeneous="true">
     <hbox>
      <entry name="timer" width-request="290" height-request="55" editable="false" xalign="0">
       <variable>TIMER</variable>
       <input>cat '$WORKDIR'/timer</input>
      </entry>
     </hbox>
    </hbox>

    <notebook  show-tabs="false" show-border="false" page="'$NOTEBOOK_TOOLS_TAB'" space-expand="true" space-fill="true">
     <frame>
      <hbox>
       <button>
        <label>12 '$(gettext "Hours")'</label>
        <action>echo clock12 > '$WORKDIR'/clock_mode</action>
       </button>
       <button>
        <label>24 '$(gettext "Hours")'</label>
        <action>echo clock24 > '$WORKDIR'/clock_mode</action>
       </button>
      </hbox>
      <hbox>
       <button>
        <label>'$(gettext "Set system time")'</label>
        <action>system_time &</action>
       </button>
       <button>
        <label>'$(gettext "Set time zone")'</label>
        <action>countrywizard &</action>
       </button> 
      </hbox>
     </frame>

     <vbox>
      <hbox>
       <button space-expand="false" space-fill="false">
        <label>" '$(gettext "Start")' "</label>
        <action>date +%s%N > '$WORKDIR'/seconds_start</action>
        <action>echo "||00:00:00" > '$WORKDIR'/resultlist</action>
        <action>echo stopwatch > '$WORKDIR'/clock_mode</action>
        <action>echo 00:00:00 > '$WORKDIR'/timer</action>
        <action>refresh:TIMER</action>
        <action>refresh:RESULTLIST</action>
        <action>enable:BUTTON_LAP</action>
        <action>enable:BUTTON_SPLIT</action>
       </button>
       <text space-expand="true" space-fill="true"><label>""</label></text>
       <checkbox>
        <label>'$(gettext "Count hundredths")'</label>
        <default>true</default>
        <action>if true echo true > '$WORKDIR'/HUNDREDTHS</action>
        <action>if false rm '$WORKDIR'/HUNDREDTHS</action>
       </checkbox>
       <button>
        <variable>BUTTON_SPLIT</variable>
        <label>" '$(gettext "Split")' "</label>
        <visible>disabled</visible>
        <action>timestamp</action>
        <action>date +%s%N > '$WORKDIR'/seconds_start</action>
        <action>echo "||00:00:00" >> '$WORKDIR'/resultlist</action>
        <action>echo 00:00:00 > '$WORKDIR'/timer</action>
        <action>refresh:RESULTLIST</action>
        <action>refresh:TIMER</action>
       </button>
       <button>
        <variable>BUTTON_LAP</variable>
        <label>" '$(gettext "Lap")' "</label>
        <visible>disabled</visible>
        <action>timestamp</action>
        <action>refresh:RESULTLIST</action>
       </button>
      </hbox>
      <tree>
       <variable>RESULTLIST</variable>
       <height>250</height><width>100</width>
       <label>"'$(gettext "Lap")'|'$(gettext "Laptime")'     |'$(gettext "Time")'"</label>
       <input>tac '$WORKDIR'/resultlist</input>
      </tree>
     </vbox>
   
     <vbox>
      <text height-request="10"><label>""</label></text>
      <hbox homogeneous="true">
      <hbox>
      <text width-request="50"><label>""</label></text>
      <button height-request="50">
       <label>"  '$(gettext "Start")'  "</label>
       <variable>BUTTON_COUNTDOWN_START</variable>
       <action>echo countdown > '$WORKDIR'/clock_mode</action>
       <action>echo "" > '$WORKDIR'/COUNTDOWN_LOOPS</action>
       <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; echo $(($(date +%s)+$TMP))$(date +%N) > '$WORKDIR'/seconds_start; echo $TMP > '$WORKDIR'/COUNTDOWN_START</action>
       <action>echo $COUNTDOWN_ACTION > /'$WORKDIR'/COUNTDOWN_ACTION</action>
       <action>enable:BUTTON_COUNTDOWN_STOP</action>
       <action>enable:BUTTON_COUNTDOWN_PAUSE</action>
       <action>disable:BUTTON_COUNTDOWN_START</action>
       <action>refresh:COUNTDOWN_LOOPS</action>
      </button>
      <checkbox label="  '$(gettext "Pause")'  " draw_indicator="false" height-request="50">
       <variable>BUTTON_COUNTDOWN_PAUSE</variable>
       <action>if true echo countdown_start > '$WORKDIR'/clock_mode</action>
       <action>if false echo countdown > '$WORKDIR'/clock_mode</action>
       <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; echo $(($(date +%s)+$TMP))$(date +%N) > '$WORKDIR'/seconds_start</action>
       <action>if true disable:BUTTON_COUNTDOWN_STOP</action>
       <action>if false enable:BUTTON_COUNTDOWN_STOP</action>
      </checkbox>
      <button label="  '$(gettext "Stop")'  " height-request="50">
       <variable>BUTTON_COUNTDOWN_STOP</variable>
       <action>echo 0 > '$WORKDIR'/COUNTDOWN</action>
       <action>echo countdown_start > '$WORKDIR'/clock_mode</action>
       <action>disable:BUTTON_COUNTDOWN_START</action>
       <action>disable:BUTTON_COUNTDOWN_PAUSE</action>
       <action>disable:BUTTON_COUNTDOWN_STOP</action>
      </button>
      <text width-request="50"><variable>COUNTDOWN_LOOPS</variable><input>cat '$WORKDIR'/COUNTDOWN_LOOPS</input></text>
      </hbox>
      </hbox>
      <text height-request="30"><label>""</label></text>








      <hbox homogeneous="true">
      <hbox>
       <vbox>
        <button label="+10">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; echo $TMP + 36000 | bc > '$WORKDIR'/COUNTDOWN</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>enable:BUTTON_COUNTDOWN_START</action>
         <action>refresh:TIMER</action>
        </button>
        <button label="+1">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; echo $TMP + 3600 | bc > '$WORKDIR'/COUNTDOWN</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>enable:BUTTON_COUNTDOWN_START</action>
         <action>refresh:TIMER</action>
        </button>
        <text use-markup="true"><label>"<b>'$(gettext "Hour")'</b>"</label></text>
        <button label="-1">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; if [ $TMP -ge 3600 ]; then echo $TMP - 3600 | bc > '$WORKDIR'/COUNTDOWN; fi</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>refresh:TIMER</action>
        </button>
        <button label="-10">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; if [ $TMP -ge 36000 ]; then echo $TMP - 36000 | bc > '$WORKDIR'/COUNTDOWN; fi</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>refresh:TIMER</action>
        </button>
       </vbox>
       <vbox>
        <button label="+10">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; echo $TMP + 600 | bc > '$WORKDIR'/COUNTDOWN</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>enable:BUTTON_COUNTDOWN_START</action>
         <action>refresh:TIMER</action>
        </button>
        <button label="+1">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; echo $TMP + 60 | bc > '$WORKDIR'/COUNTDOWN</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>enable:BUTTON_COUNTDOWN_START</action>
         <action>refresh:TIMER</action>
        </button>
        <text use-markup="true"><label>"<b>'$(gettext "Min")'</b>"</label></text>
        <button label="-1">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; if [ $TMP -ge 60 ]; then echo $TMP - 60 | bc > '$WORKDIR'/COUNTDOWN; fi</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>refresh:TIMER</action>
        </button>
        <button label="-10">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; if [ $TMP -ge 600 ]; then echo $TMP - 600 | bc > '$WORKDIR'/COUNTDOWN; fi</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>refresh:TIMER</action>
        </button>
       </vbox>
       <vbox>
        <button label="+10">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; echo $TMP + 10 | bc > '$WORKDIR'/COUNTDOWN</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>enable:BUTTON_COUNTDOWN_START</action>
         <action>refresh:TIMER</action>
        </button>
        <button label="+1">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; echo $TMP + 1 | bc > '$WORKDIR'/COUNTDOWN</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>enable:BUTTON_COUNTDOWN_START</action>
         <action>refresh:TIMER</action>
        </button>
        <text use-markup="true"><label>"<b>'$(gettext "Sec")'</b>"</label></text>
        <button label="-1">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; if [ $TMP -ge 1 ]; then echo $TMP - 1 | bc > '$WORKDIR'/COUNTDOWN; fi</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>refresh:TIMER</action>
        </button>
        <button label="-10">
         <action>TMP="`cat '$WORKDIR'/COUNTDOWN`"; if [ $TMP -ge 10 ]; then echo $TMP - 10 | bc > '$WORKDIR'/COUNTDOWN; fi</action>
         <action>COUNTDOWN_SEC="`cat '$WORKDIR'/COUNTDOWN`"; printf "%02d:%02d:%02d" $((COUNTDOWN_SEC/3600)) $((COUNTDOWN_SEC%3600/60)) $((COUNTDOWN_SEC%60)) > '$WORKDIR'/timer</action>
         <action>refresh:TIMER</action>
        </button>
       </vbox>
      </hbox>
      </hbox>









      <vbox space-expand="true" space-fill="true"><text><label>""</label></text></vbox>
      <vbox space-expand="false" space-fill="false" tooltip-text="'$(gettext "You can either play an audio-file
or execute a defined command.")'">
       <text><label>'$(gettext "Sound/Action at zero")'</label></text>
       <hbox>
        <entry accept="file" fs-title="pClock">
         <variable>COUNTDOWN_ACTION</variable>
         <input>cat '$WORKDIR'/COUNTDOWN_ACTION</input>
        </entry>
        <button>
         <input file stock="gtk-open"></input>
         <action type="fileselect">COUNTDOWN_ACTION</action>
        </button>
       </hbox>
      </vbox>
     </vbox>
     <vbox>
      <edit editable="false" left_margin="10"><input file>'$WORKDIR'/alarm_txt</input></edit>
      <button height-request="50">
       <label>'$(gettext "Define Alarm")'</label>
       <action>alarm</action>
       <action>echo $ALARM > '$WORKDIR'/ALARM</action>
      </button>
      <hbox>
       <text><label>'$(gettext "Audio")'</label></text>
       <entry accept="file">
        <variable>ALARM</variable>
        <input>cat '$WORKDIR'/ALARM</input>
       </entry>
       <button>
        <input file stock="gtk-open"></input>
        <action type="fileselect">ALARM</action>
       </button>
      </hbox>
     </vbox>
     <input file>'$WORKDIR'/pclock-tools_tab</input>
     <variable>NOTEBOOK_TOOLS</variable>
    </notebook>
   </vbox>
   <input file>'$WORKDIR'/pclock-tab</input>
   <variable>NOTEBOOK</variable>
  </notebook>
 </vbox>
 <action signal="hide">echo end > '$WORKDIR'/end_while</action>
 <action signal="hide">exit:EXIT</action>
</window>'

echo "$pClock" | sed 's/##.*//' > $WORKDIR/pclock_xml #I use double hash (##) for comments. --> as #FF0000 is valid xml-code

gtkdialog4 -f $WORKDIR/pclock_xml
echo end > $WORKDIR/end_while
